package be.fgmodelsport.ourbeers.adapters;

import android.database.DataSetObserver;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

public class ConcatListViewAdapter implements ListAdapter{
	private ListAdapter first;
	private ListAdapter second;
	
	public ConcatListViewAdapter(ListAdapter first, ListAdapter second){
		this.first = first;
		this.second = second;
	}
	
	@Override
	public int getCount() {
		return first.getCount() + second.getCount();
	}

	@Override
	public Object getItem(int pos) {
		assert pos < this.getCount();
		
		if(pos < first.getCount())
			return first.getItem(pos);
		else
			return second.getItem(pos - first.getCount());
	}

	@Override
	public long getItemId(int pos) {
		assert pos < this.getCount();
		
		Log.d("Concatlist", "id check?");
		if(pos < first.getCount())
			return first.getItemId(pos);
		else
			return first.getCount() + second.getItemId(pos - first.getCount());
	}

	@Override
	public int getItemViewType(int pos) {
		if(pos < first.getCount())
			return first.getItemViewType(pos);
		else
			return first.getViewTypeCount() + second.getItemViewType(pos - first.getCount());
	}

	@Override
	public View getView(int pos, View view, ViewGroup group) {
		if(pos < first.getCount())
			return first.getView(pos, view, group);
		else
			return second.getView(pos - first.getCount(), view, group);
	}

	@Override
	public int getViewTypeCount() {
		return  first.getViewTypeCount() + second.getViewTypeCount();
	}

	@Override
	public boolean hasStableIds() {
		return first.hasStableIds() & second.hasStableIds();
	}

	@Override
	public boolean isEmpty() {
		return first.isEmpty() & second.isEmpty();
	}

	@Override
	public void registerDataSetObserver(DataSetObserver obs) {
		first.registerDataSetObserver(obs);
		second.registerDataSetObserver(obs);
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver obs) {
		first.unregisterDataSetObserver(obs);
		second.unregisterDataSetObserver(obs);
	}

	@Override
	public boolean areAllItemsEnabled() {
		return first.areAllItemsEnabled() & second.areAllItemsEnabled();
	}

	@Override
	public boolean isEnabled(int pos) {
		if(pos < first.getCount())
			return first.isEnabled(pos);
		else
			return second.isEnabled(pos - first.getCount());
	}

}
